package com.program.facesigninsystem.controller;

import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.iteration.dto.SignInActivityByTemplate;
import com.program.facesigninsystem.iteration.dto.SignInTemplateInfo;
import com.program.facesigninsystem.iteration.dto.TemplateInfoAmend;
import com.program.facesigninsystem.iteration.entity.SignintemplateEntity;
import com.program.facesigninsystem.service.SignInTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "签到模板模块")
@RestController
@RequestMapping(value = "/template")
public class SignInTemplateController {

    @Resource
    SignInTemplateService signInTemplateService;

    @ApiOperation("获取某用户的签到模板列表")
    @GetMapping("/getList")
    public ResponseModel getTemplateList(@ApiParam(value = "用户ID",required = true)@RequestParam String uid) {

        List<SignInTemplateInfo> signInTemplateInfoList = signInTemplateService.getTemplateList(uid);
        if (signInTemplateInfoList != null)
            return new ResponseModel(200, "操作成功", signInTemplateInfoList);
        return new ResponseModel(500, "无数据或操作失败");
    }

    @ApiOperation("查看某签到模板详细信息")
    @GetMapping("/getDetailInfo")
    public ResponseModel getTemplateDetail(@ApiParam(value = "签到模板ID",required = true)@RequestParam int templateId) {

        SignintemplateEntity templateDetail = signInTemplateService.getTemplateDetail(templateId);
        if (templateDetail != null)
            return new ResponseModel(200, "操作成功", templateDetail);
        return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("创建签到模板【id不用传】")
    @PostMapping("/add")
    public ResponseModel addTemplate(@RequestBody SignintemplateEntity signintemplateEntity) {

        if (signInTemplateService.addTemplate(signintemplateEntity) == 1)
            return new ResponseModel(200, "操作成功");
        return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("删除签到模板")
    @PostMapping("/del")
    public ResponseModel delTemplate(@ApiParam(value = "被删除签到模板ID",required = true)@RequestParam int templateId) {

        if (signInTemplateService.delTemplate(templateId) == 1)
            return new ResponseModel(200, "操作成功");
        return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("修改签到模板")
    @PostMapping("/update")
    public ResponseModel updateTemplate(@RequestBody SignintemplateEntity signintemplateEntity) {

        if (signInTemplateService.updateTemplate(signintemplateEntity) == 1)
            return new ResponseModel(200, "操作成功");
        return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("获取用某签到模板创建的所有签到活动")
    @GetMapping("/getActivityListById")
    public ResponseModel getActivityListByTemplateId(@ApiParam(value = "签到模板ID",required = true)@RequestParam int templateId) {

        List<SignInActivityByTemplate> signInActivityByTemplateList = signInTemplateService.getActivityListByTemplateId(templateId);
        if (signInActivityByTemplateList != null)
            return new ResponseModel(200, "操作成功", signInActivityByTemplateList);
        return new ResponseModel(500, "无数据或操作失败");
    }

    //TODO bug1
    @ApiOperation("使用某签到模板创建签到活动")
    @PostMapping("/createActivity")
    public ResponseModel createActivity(@ApiParam(value = "签到模板ID",required = true)@RequestParam int templateId) {

        int code = signInTemplateService.createActivity(templateId);
        return codeJudge(code);
    }

    @ApiOperation("用模板的信息的修改版，创建签到活动")
    @PostMapping("/createActivity2")
    public ResponseModel createActivity2(@ApiParam(value = "签到模板ID",required = true)@RequestParam int templateId, @RequestBody TemplateInfoAmend templateInfoAmend) {

        int code = signInTemplateService.createActivity2(templateId, templateInfoAmend);
        return codeJudge(code);
    }

    @NotNull
    private ResponseModel codeJudge(int code) {
        if (code == 1)
            return new ResponseModel(200, "创建成功", "积分 +30");
        if (code == 2)
            return new ResponseModel(200, "创建成功", "今日获取总积分已达上限");
        if (code == 3)
            return new ResponseModel(200, "创建成功", "今日创建活动获取积分次数已达上限");
        return new ResponseModel(500, "操作失败");
    }
}
